class HealthNews {
  final String id;
  final String title;
  final String description;
  final String source;
  final String ctime;
  final String picUrl;
  final String url;

  HealthNews({
    required this.id,
    required this.title,
    required this.description,
    required this.source,
    required this.ctime,
    required this.picUrl,
    required this.url,
  });

  factory HealthNews.fromJson(Map<String, dynamic> json) {
    return HealthNews(
      id: json['id'] ?? '',
      title: json['title'] ?? '',
      description: json['description'] ?? '',
      source: json['source'] ?? '',
      ctime: json['ctime'] ?? '',
      picUrl: json['picUrl'] ?? '',
      url: json['url'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'source': source,
      'ctime': ctime,
      'picUrl': picUrl,
      'url': url,
    };
  }
}

class HealthNewsResponse {
  final int curpage;
  final int allnum;
  final List<HealthNews> news;

  HealthNewsResponse({
    required this.curpage,
    required this.allnum,
    required this.news,
  });

  factory HealthNewsResponse.fromJson(Map<String, dynamic> json) {
    final result = json['result'];

    // 处理不同的 API 响应格式 (newslist 或 list)
    final List<dynamic> newsItems = result['newslist'] ?? result['list'] ?? [];

    return HealthNewsResponse(
      curpage: result['curpage'] ?? 1,
      allnum: result['allnum'] ?? 0,
      news: List<HealthNews>.from(
          newsItems.map((item) => HealthNews.fromJson(item))),
    );
  }
}
